import { Injectable } from '@angular/core';
import { StateMachineService } from "@farris/command-services";
import { FrameContext } from '@farris/devkit';
import { BefRepository, ResponseInfo } from '@farris/bef';
import { FormMessageService, FormNotifyService, CommandService, FormLoadingService, BeActionService } from '@farris/command-services';
import { EMPTY, of } from 'rxjs';
import { tap, map, switchMap } from 'rxjs/operators';

@Injectable()
export class NewsbbyjhFrmControllerService {
  constructor(
    private frameContext: FrameContext,
    private messageService: FormMessageService,
    private beActionService: BeActionService,
    public stateMachineService: StateMachineService,
    public formNotifyService: FormNotifyService,
    private commandService: CommandService,
    public loadingService: FormLoadingService
  ) { }

  /**
   * 生成下年计划点击事件
   * @remarks 
   * @returns 
   */
  annualPlan(): any {
    var year = this.frameContext.uiState['currentQueryConditions'][0].value.textValue;
    if (year === 'help-text-value-null') {
      this.messageService.error("请在筛选框中选择年份!")
      return EMPTY;
    }
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'annualplan';
    const body = {
      year: year,
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => { })
    )
  }


  /**
 * 初始化保养计划
 * @remarks 
 * @returns 
 */
  initAnnualPlan(): any {
    const repository = this.frameContext.repository as BefRepository<any>;
    const requestInfo = repository.restService.buildRequestInfo();
    const actionUri = 'initannualplan';
    const body = {
      requestInfo: requestInfo
    }
    const action$ = this.beActionService.invokeAction(actionUri, 'PUT', null, null, body);
    return action$.pipe(
      tap((responseInfo: ResponseInfo) => { })
    )
  }

  /**
   * 组件视图初始化后事件
   * @remarks 
   * @returns 
   */
  componentAfterViewInit(): any {
    const currentYear: string = new Date().getFullYear().toString();
    const defaultValue = {
      fieldConfigs: [
        {//下拉
          labelCode: "year",
          value: currentYear
        },
      ]
    }
    this.frameContext.uiState['queryScheme1Binding'] = defaultValue
  }
}
